package com.zhihuishu.micro.course.openapi.course.impl;

import com.zhihuishu.micro.course.dto.LessonDto;
import com.zhihuishu.micro.course.openapi.course.CourseLessonOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseLessonDto;
import com.zhihuishu.micro.course.service.LessonTreenityService;
import com.zhihuishu.remote.ErrorCode;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Jiangli
 * @date 2016/10/17 15:27
 */
@Service("courseLessonOpenService")
public class CourseLessonOpenServiceImpl extends BaseOpenImpl implements CourseLessonOpenService {
    @Resource
    private LessonTreenityService lessonAllService;//所有节 service
    private static final String moduleNameCN = "节";

    @Override
    public RemoteResult<CourseLessonDto> createByDto(CourseLessonDto courseChapterDto, long createUserId) throws RemoteException {
        RemoteResult<CourseLessonDto> result = new RemoteResult<CourseLessonDto>();
        try {
            LessonDto dbDto = BeanHelper.transTo(courseChapterDto, LessonDto.class);

            Integer newId = lessonAllService.create(dbDto, createUserId);
            dbDto.setId(newId);

            CourseLessonDto resultDto = BeanHelper.transTo(dbDto, CourseLessonDto.class);

            result.setSuccess(true);
            result.setResult(resultDto);
        } catch (Exception e) {
            log.error("保存-" + moduleNameCN + "-报错");
            throw new RemoteException(e).setMessage("保存-" + moduleNameCN + "-报错!");
        }

        return result;
    }

    @Override
    public RemoteResult<Void> updateByDto(CourseLessonDto chapter, long updateUserId) throws RemoteException {
        RemoteResult<Void> result=new RemoteResult<Void>();
        if(chapter==null){
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
            LessonDto chapterDto = BeanHelper.transTo(chapter, LessonDto.class);
            lessonAllService.update(chapterDto,updateUserId);

            result.setSuccess(true);
        } catch (Exception e) {
            throw new RemoteException(e).setMessage("修改-" + moduleNameCN + "-出错!") ;
        }
        return result;
    }

    @Override
    public RemoteResult<Void> deleteById(Integer chapterId, long deleteUserId) throws RemoteException {
        RemoteResult<Void>  result = new RemoteResult<Void>();
        if(chapterId == null){
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result ;
        }
        try {
            lessonAllService.delete(chapterId,deleteUserId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RemoteException(e).setMessage("删除-" + moduleNameCN + "-出错!") ;
        }
        return result;
    }

    @Override
    public RemoteResult<List<CourseLessonDto>> queryListByCourseId(long courseId) throws RemoteException {
        RemoteResult<List<CourseLessonDto>> result=new RemoteResult<List<CourseLessonDto>>();
        try {
            List<LessonDto> chapterDtoList = lessonAllService.list(courseId);
            List<CourseLessonDto> courseOpenDtoList =  new ArrayList<CourseLessonDto>();
            if (!CollectionUtils.isEmpty(chapterDtoList)){
                for(LessonDto course : chapterDtoList){
                    CourseLessonDto courseOpenDto = BeanHelper.transTo(course, CourseLessonDto.class);
                    courseOpenDtoList.add(courseOpenDto);
                }
            }
            result.setResult(courseOpenDtoList);
        } catch (Exception e) {
            result.setSuccess(false);
            throw new RemoteException(e).setMessage("查询-" + moduleNameCN + "列表-出错!") ;
        }
        return result;
    }

    @Override
    public RemoteResult<Void> sortByDtoList(List<CourseLessonDto> chapterList, long updateUserId) throws RemoteException {
        RemoteResult<Void> result=new RemoteResult<Void>();
        if(CollectionUtils.isEmpty(chapterList)){
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
            List<LessonDto> dbDtoList = new LinkedList<LessonDto>();
            for (CourseLessonDto courseChapterDto : chapterList) {
                dbDtoList.add(BeanHelper.transTo(courseChapterDto, LessonDto.class));
            }

            lessonAllService.sort(dbDtoList,updateUserId);

            result.setSuccess(true);
        } catch (Exception e) {
            throw new RemoteException(e).setMessage("排序-" + moduleNameCN + "-出错!") ;
        }
        return result;
    }
}
